<?php
	class paises extends Model {
		private $mTableName = 'GN_Paises';

		public function __construct() {
			parent::Model();
			$this->load->database();
		}

		function CreatePais($nombre) {
			if (empty($nombre) )
				return false;

			$data = array(
				'NombrePais'	=> $nombre
			);

			$this->db->insert($this->mTableName, $data);
			return $this->LoadPaisById($this->db->insert_id());
		}

		function UpdatePais($pais) {
			$nombre = $pais->GetNombre();

			if (empty($nombre) )
				return false;

			$data = array(
				'NombrePais'	=> $nombre
			);

			$this->db->where('IdPais', $pais->GetIdPais());
        	$this->db->update($this->mTableName, $data);
		}

		function LoadPaisById($id_pais) {
			$qresult = $this->db->get_where($this->mTableName, array('IdPais' => $id_pais));

			if ($qresult->num_rows() == 0)
				return false;

			return new pais($qresult->row());
		}

		function LoadPaises($start = false, $offset = false, $sortname = false, $sortorder = false) 
		{
			if($offset != false)
				$this->db->limit($offset, $start);

			if($sortname != false && $sortorder != false) 
			{
				$this->db->order_by('IdPais', 'asc');
			}

			$result = $this->db->get_where($this->mTableName);

			return $this->GetPaisesArrayFromQueryResult($result);
		}

		function GetCount() {
			return count($this->LoadPaises());
		}

		function GetPaisesArrayFromQueryResult($result) {
			if ($result->num_rows() == 0)
				return false;

			$paises_arr = array();

			foreach ($result->result() as $record) {
				$paises_arr[] = new pais($record);
			}

			return $paises_arr;
    	}
	}

	class pais {
		private $mIdPais = 0;

		private $mNombre = "";

		function __construct($object) {
			$this->SetIdPais($object->IdPais);
			$this->SetNombre($object->NombrePais);
		}

		private function SetIdPais($id_pais) {
			$this->mIdPais = $id_pais;
		}

		function GetIdPais() {
			return $this->mIdPais;
		}

		function SetNombre($nombre) {
			$this->mNombre = $nombre;
		}

		function GetNombre() {
			return $this->mNombre;
		}
	}
?>